package com.ruoyi.paperDetail.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.paper.domain.Paper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.paperDetail.mapper.PaperDetailMapper;
import com.ruoyi.paperDetail.domain.PaperDetail;
import com.ruoyi.paperDetail.service.IPaperDetailService;

/**
 * 论文信息内容展示Service业务层处理
 * 
 * @author moyu
 * @date 2023-03-07
 */
@Service
public class PaperDetailServiceImpl implements IPaperDetailService 
{
    @Autowired
    private PaperDetailMapper paperDetailMapper;

    /**
     * 查询论文信息内容展示
     * 
     * @param id 论文信息内容展示主键
     * @return 论文信息内容展示
     */
    @Override
    public PaperDetail selectPaperDetailById(Long id)
    {
        return paperDetailMapper.selectPaperDetailById(id);
    }

    /**
     * 查询论文信息内容展示列表
     * 
     * @param paperDetail 论文信息内容展示
     * @return 论文信息内容展示
     */
    @Override
    public List<PaperDetail> selectPaperDetailList(PaperDetail paperDetail)
    {
        return paperDetailMapper.selectPaperDetailList(paperDetail);
    }

    /**
     * 新增论文信息内容展示
     * 
     * @param paperDetail 论文信息内容展示
     * @return 结果
     */
    @Override
    public int insertPaperDetail(PaperDetail paperDetail)
    {
        return paperDetailMapper.insertPaperDetail(paperDetail);
    }

    /**
     * 修改论文信息内容展示
     * 
     * @param paperDetail 论文信息内容展示
     * @return 结果
     */
    @Override
    public int updatePaperDetail(PaperDetail paperDetail)
    {
        return paperDetailMapper.updatePaperDetail(paperDetail);
    }

    /**
     * 批量删除论文信息内容展示
     * 
     * @param ids 需要删除的论文信息内容展示主键
     * @return 结果
     */
    @Override
    public int deletePaperDetailByIds(Long[] ids)
    {
        return paperDetailMapper.deletePaperDetailByIds(ids);
    }

    /**
     * 删除论文信息内容展示信息
     * 
     * @param id 论文信息内容展示主键
     * @return 结果
     */
    @Override
    public int deletePaperDetailById(Long id)
    {
        return paperDetailMapper.deletePaperDetailById(id);
    }

    @Override
    public String importPaper(List<PaperDetail> paperDetailList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(paperDetailList) || paperDetailList.size() == 0)
        {
            throw new ServiceException("导入论文基本信息数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        //清洗数据
        List<PaperDetail> paperDetail_clean = cleanData(paperDetailList);
        //遍历
        for (PaperDetail paperDetail:paperDetail_clean){
            List<PaperDetail> pdl = paperDetailMapper.selectPaperDetailList(paperDetail);
            //论文不存在，存入数据库
            if (StringUtils.isNull(pdl) || pdl.size() == 0){
                int i = paperDetailMapper.insertPaperDetail(paperDetail);
                if (i!=0) {
                    successNum++;
                }else {
                    failureNum++;
                }
            }else {
                failureNum++;
            }
        }
        String Msg = "导入成功"+successNum+"条数据，失败了"+failureNum+"条数据";

        return Msg;
    }

    /**
     * 清洗数据，去除换行符；专辑：；分类号：
     * @return
     */
    public List<PaperDetail> cleanData(List<PaperDetail> paperDetailList){
        List<PaperDetail> paperDetails = new ArrayList<PaperDetail>();
        for (PaperDetail paperDetail : paperDetailList){
            String keyword = paperDetail.getKeyword();
            //关键字：去除换行符,空格，最后一个分号
            String keyword_now = keyword.replaceAll("\n", "")
                    .replaceAll(" ","");
            String substring = keyword_now.substring(0, keyword_now.length() - 1);

            paperDetail.setKeyword(substring);
            //DOI
            String doi = paperDetail.getDOI();
            String doi_HuanHangFu = doi.replaceAll("\n", "");
            if (doi_HuanHangFu.substring(0,2).equals("专辑")){
                String doi_zhuan = doi_HuanHangFu.replaceAll("专辑：", "");
                paperDetail.setDOI(doi_zhuan);
            }else if (doi_HuanHangFu.substring(0,3).equals("DOI")) {
                String doi_DOI = doi_HuanHangFu.replaceAll("DOI：", "");
                paperDetail.setDOI(doi_DOI);
            }else {
                paperDetail.setDOI(doi_HuanHangFu);
            }
            //分类号
            String typeNumber = paperDetail.getTypeNumber();
            String type_now = typeNumber.replaceAll("分类号：", "").replaceAll("\n", "");
            paperDetail.setTypeNumber(type_now);

            paperDetails.add(paperDetail);

        }

        return paperDetails;
    }
}
